var graceChecker = require("../../graceUI/jsTools/graceChecker.js");
const req = require('../../req/index.js');
var app = getApp();
Page({
	data: {
		top: 0,
		vcodeBtnName: "获取验证码",
		countNum: 0,
		countDownTimer: null,
		phoneno: '',
		errorMsg: '',
		successMsg : '',
		pageLoading : false
	},
	onLoad(){
		this.setData({
			// top: wx.getSystemInfoSync().statusBarHeight + 44
		})
	},
	goback() {
		/* wx.switchTab({
			url: '../my/my'
		}) */
		wx.navigateBack();
	},
	phonenoInput: function(e) {
		this.setData({
			phoneno: e.detail.value
		});
	},
	successShow(msg){
		this.setData({
			successMsg: msg
		});
		this.selectComponent('#errorMsg').close();
		this.selectComponent('#successMsg').open();	
	},
	errorShow(msg){
		this.setData({
			errorMsg: msg
		});
		this.selectComponent('#successMsg').close();	
		this.selectComponent('#errorMsg').open();
	},
	getVCode: function() {
		var myreg = /^[1][0-9]{10}$/;
		if (!myreg.test(this.data.phoneno)) {
			this.errorShow("请正确填写手机号码");
			return false;
		}
		// vcodeBtnName 可以阻止按钮被多次点击 多次发送 return 会终止函数继续运行
		if (this.data.vcodeBtnName != '获取验证码' && this.data.vcodeBtnName != '重新发送') {
			return;
		}
		this.setData({
			vcodeBtnName: "发送中..."
		});
		// 与后端 api 交互，发送验证码 
		req.user.getbingCode(this.data.phoneno)
			.then((res) => {
				if(res.head.code == 1){
					this.successShow("短信已发送，请注意查收");
				}else{
					this.errorShow(res.head.message);
				}
				// 倒计时
				this.setData({
					countNum: 100
				});
				this.setData({
					countDownTimer: setInterval(function() {
						this.countDown();
					}.bind(this), 1000)
				});
			})
			.catch((err)=>{
				req.err.show(err);
				this.setData({
					vcodeBtnName: "获取验证码"
				});
			})
	},
	countDown: function() {
		if (this.data.countNum < 1) {
			clearInterval(this.data.countDownTimer);
			this.setData({
				vcodeBtnName: "重新发送"
			});
			return;
		}
		this.data.countNum--;
		this.setData({
			countNum: this.data.countNum,
			vcodeBtnName: this.data.countNum + '秒重发'
		});
	},
	// 表单提交及验证
	formSubmit: function(e) {
		//定义表单规则
		var rule = [{
				name: "name",
				checkType: "notnull",
				checkRule: "",
				errorMsg: "请输入姓名"
			},
			{
				name: "zjhm",
				checkType: "notnull",
				checkRule: "",
				errorMsg: "请输入证件号码"
			},
			{
				name: "sjhm",
				checkType: "notnull",
				checkRule: "",
				errorMsg: "请输入手机号码"
			},
			{
				name: "sjhm",
				checkType: "phoneno",
				checkRule: "",
				errorMsg: "手机号码格式错误"
			},
			{
				name: "sjyzm",
				checkType: "notnull",
				checkRule: "",
				errorMsg: "请输入验证码"
			},
			{
				name: "sjyzm",
				checkType: "string",
				checkRule: "6,6",
				errorMsg: "验证码长度错误"
			}
		];
		//进行表单检查 e.detail.value 内存放着表单数据
		var formData = e.detail.value;
		console.log(formData);
		var checkRes = graceChecker.check(formData, rule);
		if (checkRes) {
			this.setData({
				pageLoading: true
			});
			req.user.savexcxUserInfo(formData.name, formData.zjhm, formData.sjhm, formData.sjyzm)
				.then((res) => {
					if(res.head.code == 1){
						this.successShow("绑定成功");
						app.globalData.bindingInfoFlag = true;
						setTimeout(()=>{
							wx.navigateBack();
						},1000)
					}else{
						this.errorShow(res.head.message);
					}
				})
				.catch(req.err.show)
				.then(()=>{
					this.setData({
						pageLoading: false
					});
				})
		} else {	
			this.errorShow(graceChecker.error);
			/* wx.showToast({
				title: graceChecker.error,
				icon: "none"
			}); */
		}
	}
});
